{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-end">
        <button type="button" data-bs-toggle="tooltip" title="{{ button_filter }}" onclick="$('#filter-task').toggleClass('d-none');" class="btn btn-light d-lg-none"><i class="fa-solid fa-filter"></i></button>
        <button type="button" id="button-refresh" data-bs-toggle="tooltip" title="{{ button_refresh }}" class="btn btn-light"><i class="fa-solid fa-sync"></i></button>
        <button type="button" id="button-start" data-bs-toggle="tooltip" title="{{ button_run }}" class="btn btn-success"><i class="fa-solid fa-play"></i></button>
        <button type="button" id="button-pause" data-bs-toggle="tooltip" title="{{ button_pause }}" class="btn btn-warning"><i class="fa-solid fa-pause"></i></button>
        <button type="button" id="button-clear" data-bs-toggle="tooltip" title="{{ button_clear }}" class="btn btn-danger"><i class="fa-solid fa-eraser"></i></button>
        <div class="btn-group">
          <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown"><i class="fa-solid fa-list-check"></i> {{ button_action }} <i class="fa-solid fa-caret-down fa-fw"></i></button>
          <ul class="dropdown-menu">
            <li><h6 class="dropdown-header">{{ text_status }}</h6></li>
            <li><button type="submit" form="form-task" formaction="{{ pending }}" class="dropdown-item"><i class="fa-solid fa-circle-exclamation text-secondary"></i> {{ text_pending }}</button></li>
            <li><button type="submit" form="form-task" formaction="{{ processing }}" class="dropdown-item"><i class="fa-solid fa-circle-exclamation text-primary"></i> {{ text_processing }}</button></li>
            <li><button type="submit" form="form-task" formaction="{{ paused }}" class="dropdown-item"><i class="fa-solid fa-circle-exclamation text-warning"></i> {{ text_paused }}</button></li>
            <li><button type="submit" form="form-task" formaction="{{ complete }}" class="dropdown-item"><i class="fa-solid fa-circle-exclamation text-success"></i> {{ text_complete }}</button></li>
            <li><button type="submit" form="form-task" formaction="{{ failed }}" class="dropdown-item"><i class="fa-solid fa-circle-exclamation text-danger"></i> {{ text_failed }}</button></li>
            <li>
              <hr class="dropdown-divider">
            </li>
            <li><button type="submit" form="form-task" formaction="{{ delete }}" onclick="return confirm('{{ text_confirm }}');" class="dropdown-item"><i class="fa-regular fa-trash-can text-danger"></i> {{ text_delete }}</button></li>
          </ul>
        </div>
      </div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    <div class="row">
      <div id="filter-task" class="col-lg-3 col-md-12 order-lg-last d-none d-lg-block mb-3">
        <div class="card">
          <div class="card-header"><i class="fa-solid fa-filter"></i> {{ text_filter }}</div>
          <div class="card-body">
            <form id="form-filter">
              <div class="mb-3">
                <label for="input-code" class="form-label">{{ entry_code }}</label>
                <input type="text" name="filter_code" value="{{ filter_code }}" placeholder="{{ entry_code }}" id="input-code" class="form-control"/>
              </div>
              <div class="mb-3">
                <label for="input-status" class="form-label">{{ entry_status }}</label>
                <select name="filter_status" id="input-status" class="form-select">
                  <option value=""></option>
                  {% for status in statuses %}
                    <option value="{{ status.value }}">{{ status.text }}</option>
                  {% endfor %}
                </select>
              </div>
              <div class="text-end">
                <button type="submit" id="button-filter" class="btn btn-light"><i class="fa-solid fa-filter"></i> {{ button_filter }}</button>
                <button type="reset" data-bs-toggle="tooltip" title="{{ button_reset }}" class="btn btn-outline-secondary"><i class="fa-solid fa-filter-circle-xmark"></i></button>
              </div>
            </form>
          </div>
        </div>
      </div>
      <div class="col col-lg-9 col-md-12">
        <div class="card">
          <div class="card-header"><i class="fa-solid fa-list"></i> {{ text_list }}</div>
          <div id="list" class="card-body">{{ list }}</div>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#form-filter').on('submit', function(e) {
    e.preventDefault();

    let url = $(this).serialize();

    window.history.pushState({}, null, 'index.php?route=marketplace/task&user_token={{ user_token }}&' + url);

    $('#list').load('index.php?route=marketplace/task.list&user_token={{ user_token }}&' + url);
});

$('#button-refresh').on('click', function(e) {
    $.ajax({
        url: $('#form-task').attr('data-oc-load'),
        dataType: 'html',
        beforeSend: function() {
            $('#button-refresh').button('loading');
        },
        complete: function() {
            $('#button-refresh').button('reset');
        },
        success: function(html) {
            $('#list').html(html);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#button-start').on('click', function(e) {
    $.ajax({
        url: 'index.php?route=marketplace/task.start&user_token={{ user_token }}',
        dataType: 'json',
        beforeSend: function() {
            $('#button-start').button('loading');
        },
        complete: function() {
            $('#button-start').button('reset');
        },
        success: function(json) {
            console.log(json);

            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                window.setTimeout(() => {
                    $('#list').load($('#form-task').attr('data-oc-load'));
                }, 3000);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#button-pause').on('click', function(e) {
    $.ajax({
        url: 'index.php?route=marketplace/task.pause&user_token={{ user_token }}',
        dataType: 'json',
        beforeSend: function() {
            $('#button-pause').button('loading');
        },
        complete: function() {
            $('#button-pause').button('reset');
        },
        success: function(json) {
            console.log(json);

            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                $('#list').load($('#form-task').attr('data-oc-load'));
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$('#button-clear').on('click', function(e) {
    $.ajax({
        url: 'index.php?route=marketplace/task.clear&user_token={{ user_token }}',
        dataType: 'json',
        beforeSend: function() {
            $('#button-clear').button('loading');
        },
        complete: function() {
            $('#button-clear').button('reset');
        },
        success: function(json) {
            console.log(json);

            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                $('#list').load($('#form-task').attr('data-oc-load'));
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});
//--></script>
{{ footer }}